package com.teddy.DTXMB.Controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.teddy.DTXMB.Model.ShoppingCart;
import com.teddy.DTXMB.Model.ShoppingCartList;
import com.teddy.DTXMB.Model.Custom;
import com.teddy.DTXMB.Model.Order;
import com.teddy.DTXMB.Model.OrderList;
import com.teddy.DTXMB.Service.OrderService;

@Controller
@SessionAttributes("custom")
public class OrderController {
	/*Log实例*/
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
	
	@Autowired
	private OrderService orderService;

	// 这个是个"难点", List传入Action的时候要先把这个List变成非List类. 另外，Mapping中只允许有一个@ModelAttribute, 如果有两个则有一个是不能用的.
	@RequestMapping(value = "/order/make", method = RequestMethod.POST)
	public String MakeOrder(@ModelAttribute ShoppingCartList sCarts, Model model, HttpServletRequest request){
		/*判断Session内容*/
		if(request.getSession().getAttribute("custom") != null){
			Custom custom = (Custom) request.getSession().getAttribute("custom");
			/*判断custom合法性*/
			if(custom.getName() != null){
				List<ShoppingCart> tempCart = sCarts.getQuery();
				if(tempCart.size() > 0){
					int newOrderID = orderService.makeShoppingCartToOrder(custom, tempCart);
					/*生成新的OrderID，若成功则跳到Order页面，失败则跳到错误页面*/
					logger.info("New Order ID: " +  Integer.toString(newOrderID));
					if(newOrderID != 0){
						return "redirect:/order/" + newOrderID;
					}
					else{
						return "error";
					}
				}
				else{
					return "error";
				}
			}
			else{
				return "errorcustom";
			}	
		}
		else
		{
			return "errorcustom";
		}
	}
	
	/*避免直接请求此URL(/order/make)*/
	@RequestMapping(value = "/order/make", method = RequestMethod.GET)
	public String MakeOrder2(Model model, HttpServletRequest request){
		return "redirect:/";  
	}
	
	/*订单详情*/
	@RequestMapping(value = "/order/{orderid}", method = RequestMethod.GET)
	public String ShowOrderDetail(@PathVariable(value="orderid") int orderID, HttpServletRequest request, Model model){
		if(request.getSession().getAttribute("custom") != null){
			Custom custom = (Custom) request.getSession().getAttribute("custom");
			if(custom.getName() != null){
				Order order = orderService.getOrderByID(orderID);
				List<OrderList> orderLists = orderService.getOrderListByOrderID(orderID);
				model.addAttribute("order", order);
				model.addAttribute("orderLists", orderLists);
				return "orderdetail";
			}
			else {
				return "errorcustom";
			}
		}
		else{
			return "errorcustom";
		}
	}
	
	/*订单分页*/
	@RequestMapping(value = "/order/page/{pageid}", method = RequestMethod.GET)
	public String getOrderByPageID(@PathVariable(value="pageid") int pageNumber, HttpServletRequest request, Model model){
		if(request.getSession().getAttribute("custom") != null){
			Custom custom = (Custom) request.getSession().getAttribute("custom");
			if(custom.getName() != null){
				int customID = custom.getId();
				List<Order> orders = orderService.getAllOrderByCustomID(customID);
				
				// 取得所有订单数量
				int ordersNumber = orders.size();
				// 每页5个，可以算出页数
				int totalPage = ordersNumber / 5;
				if(ordersNumber % 5 > 0){
					totalPage = totalPage + 1;
				}
				// 构造一个List，用来存储页码
				List<Integer> listPage = new ArrayList<Integer>();
				// 因为是页码，要从1开始
				for (int i=1;i<totalPage + 1; i++){
					listPage.add(i);
				}		
				/*取到此页的Order*/
				List<Order> orderss = orderService.getOrdersByPageID(customID,pageNumber - 1);
				/*向JSP页面中输出页码，订单，总页数，当前页数*/
				model.addAttribute("listPage", listPage);
				model.addAttribute("orders", orderss);
				model.addAttribute("pageNumber",totalPage);
				model.addAttribute("currentPage", pageNumber);
				return "order";
			}
			else{
				return "errorcustom";
			}
		}
		else{
			return "errorcustom";
		}
	}
}
